const mysql = require("mysql");
const http = require("http");
const fs = require("fs");
const path = require("path");

http.createServer((req, res) => {
    var urlObj = new URL(req.url, "http://localhost:8084/")
    var pathName = urlObj.pathname;
    console.log(req.url)
    if (pathName == '/') {
        var filePath = path.join(__dirname, "/chapterList.html")
        var fileContent = fs.readFileSync(filePath)
        res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
        res.write(fileContent);
        res.end()
    }
    else if (pathName == '/getChapterList') {
        // 从数据库读取数据
        var con = mysql.createConnection({
            host: '192.168.16.144', //虚拟机的IP
            port: 3306, //默认端口3306
            user: 'root',
            password: 'ddd',
            database: 'dataexercise' //链接的数据库名
        })
        var selectSql = "select * from chapters"
        //如果没有?,则不用传第二个参数
        con.query(selectSql, (err, result) => {
            if (err) { console.log(err) }
            else {
                
                var dataList = JSON.stringify(result); //把数组转换为JSON对象
                res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" })
                res.write(dataList);
                res.end();
            }
        })
    } 
    else if (pathName.indexOf('/css/') >= 0) {
        console.log(pathName.indexOf('/css/'))
        var filePath = path.join(__dirname, pathName)
        var fileContent = fs.readFileSync(filePath)
        res.writeHead(200, { "Content-Type": "text/css;charset=utf-8" })
        res.write(fileContent);
        res.end();
    }
    else if (pathName.indexOf('images/') >= 0) {
        var filePath = path.join(__dirname, '/' + pathName)
        var fileContent = fs.readFileSync(filePath)
        if(pathName.indexOf('.png')>=0){
            res.writeHead(200, { "Content-Type": "image/png" })
            res.write(fileContent);
            res.end();
        }
        else if(pathName.indexOf('.jpg')>=0){
            res.writeHead(200, { "Content-Type": "image/jpeg" })
            res.write(fileContent);
            res.end();
        }
    }
    else if(pathName.indexOf("/detail")>=0){
        
        var filePath = path.join(__dirname, "/chapter.html");
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" })
        res.write(fileContent);
        res.end();
    }
    else if(pathName.indexOf("/getDetail")>=0){
        var chapterId=req.url.split('=')[1]; 
        //例如：获取http://localhost:8084/detail?chapterId=0 由‘=’分开数据的第2个数据，第一个是=，第二个是0
        var con = mysql.createConnection({
            host: '192.168.16.144', //虚拟机的IP
            port: 3306, //默认端口3306
            user: 'root',
            password: 'ddd',
            database: 'dataexercise' //链接的数据库名
        })
        var selectSql = "select * from chapters where chapterId=?"
        con.query(selectSql, [chapterId],(err, result) => {
            if (err) { console.log(err) }
            else {
                result = JSON.stringify(result); //把数组转换为JSON对象
                res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" })
                res.write(result);
                res.end();
            }
        })
    }
    else {
        res.end("");
    }
}).listen(8084);



